<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();
class Parser_Tag_list extends Parser {
	public $tags_inside = true;
	public $allowBr = false;
	
	public function get($inner,$param = ''){
	
		$list = trim($inner);

		switch ($param) {
			case 'ul':
				$op = '<ul>';
				$cl = '</ul>';
				break;
			case 'ol':
			default:
				$op = '<ol>';
				$cl = '</ol>';
				break;
		}
		
		if (strpos($inner,'[*]') !== false) {
			$listA = explode('[*]',$list);
		} else {
			$listA = explode(N,$list);
		}
		
		array_clean($listA);
		
		$list = '';
		foreach ($listA as $l) {
			if (trim($l) !== '<br />') {
				$list .= '<li>'.$l.'</li>';
			}
		}

		return $op.'<!--list-->'.$list.'<!--/list-->'.$cl;
	}
	public function unparse($text){
		return preg_replace('~\<(ul|ol)\>\<\!--list--\>(.+?)\<\!--\/list--\>\<\/(\\1)\>~ise',"self::unList('\\2','\\1')",$text);
	}
	private function unList($text,$l){
		$text = trim($text);
		if (substr($text,-5) == '</li>') {
			$text = substr($text,0,-5);
		}
		if (substr($text,0,4) == '<li>') {
			$text = substr($text,4);
		}
		$ta = explode('<li>',$text);
		$ret = '';
		foreach ($ta as $t){
			
			if (($p = substr( trim($t),-5)) == '</li>') {
				$t = substr(trim($t),0,-5);
			} else {
				$t = trim($t);
			}
			$ret .= RN.'[*]'.trim($t);
		}
		return '[list='.$l.']'.$ret.RN.'[/list]';
	}
}
